<?php

/**
 * CodeIgniter Captcha Class
 *
 * 用户验证类
 */
class CT_auth_member_teacher
{
    private $ci;
    
    public function __construct()
    {
        $this->ci = & get_instance();
        $this->ci->load->library('session');
    }
    
    
    public function set_user_login($userinfo)
    {
        $user = array();
        if(isset($userinfo['id'])){
            $user['uid'] = $userinfo['id'];
            $user['token'] = $this->hashSSHA($userinfo['id']);
        }
        if(isset($userinfo['nursery_id'])){
            $user['nursery_id'] = $userinfo['nursery_id'];
        }
        if(isset($userinfo['wx_open_id'])){
            $user['open_id'] = $userinfo['wx_open_id'];
        }
        if(isset($userinfo['is_teacher'])){
            $user['is_teacher'] = $userinfo['is_teacher'];
        }
       
        $this->ci->session->set_userdata($user);
    }
    
    /**
     * 获取用户登录信息
     */
    public function get_user_login()
    {
        return $this->ci->session->userdata();
    }

    public function is_login()
    {
        
        if ($this->ci->session->userdata()) 
        {
            $token = $this->ci->session->userdata('token');
            $uid = $this->ci->session->userdata('uid');
            
            if ($this->hashSSHA($uid) == $token) 
            {
                return $this->get_user_login();
            }
        }
        return false;
    }

    /**
     * 用户登出、清除cookie
     */
    public function set_user_logout()
    {
        return $this->ci->session->sess_destroy();
    }

    /**
     * 用户token生成方法
     * @param int $uid            
     * @return string
     */
    public function hashSSHA($uid)
    {
        $salt = substr($uid, 0, 4);
        $hash = base64_encode(sha1($uid . $salt, true) . $salt);
        return $hash;
    }
}
